/**
 * 
 */
package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Route;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.bean.Unit;
import com.efast.dmom.mes.bean.WorkCenter;
import com.efast.dmom.mes.bean.base.IModificationFlags;
import com.efast.dmom.mes.bean.base.ITransactionNames;
import com.efast.dmom.mes.dao.entity.UnitDao;
import com.efast.dmom.mes.datahandlers.base.RuntimeDataHandler;
import com.efast.dmom.mes.entity.UnitEntity;

/**
 * @author Aaron Liu
 *
 *         2020年4月7日
 */
@Component
public class UnitDataHandler extends RuntimeDataHandler<Unit> {

	@Autowired
	private UnitDao unitDao = null;
	
	@Autowired
	private RouteDataHandler routeDataHandler = null;
	
	@Autowired
	private RouteStepDataHandler routeStepDataHandler = null;

	public void saveUnit(List<Unit> list) {

		List<UnitEntity> entityList = new ArrayList<UnitEntity>();

		if(list != null){
			
			for (Unit unit : list) {

				if (unit.getModificationFlag() == IModificationFlags.NEW) {

					entityList.add(unit.getInnerData());
				}
			}
		}
		
		if(entityList.size() > 0){
			unitDao.saveUnit(entityList);
		}
	}
	
	public ResultBean startAtWorkCenter(long unitKey, String stepName, String workCenterName, String comments) {

		ResultBean resultBean = new ResultBean();

		UnitEntity unitEntity = unitDao.getUnitByKey(unitKey);
		if (unitEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("Can not retrive unit by key: " + unitKey);
			return resultBean;
		}

		Unit unit = new Unit();
		unit.setInnerData(unitEntity);

		boolean isValid = unit.validateTransaction(unit, ITransactionNames.TRX_NAME_UNITSTART);
		if (!isValid) {
			resultBean.setSuccess(false);
			resultBean.setMessage("Unit: " + unitEntity.getSerialNumber() + " is not in a valid status");
			return resultBean;
		}
		
		long routeKey = unitEntity.getRouteKey();
		// TODO ????
		Route route = routeDataHandler.getRouteByKey(routeKey);
		if (route == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("Can not retrive Route by key: " + unitEntity.getRouteKey());
			return resultBean;
		}
		// TODO
		RouteStep routeStep = routeStepDataHandler.getRouteStepByName(routeKey, stepName);
		if(routeStep == null){
			resultBean.setSuccess(false);
			resultBean.setMessage("There's no RouteStep on Route : " + route.getInnerData().getName());
			return resultBean;
		}
		
		long stepKey = routeStep.getInnerData().getId();
		boolean isAttached = false;
		
		if(workCenterName == null || "".equals(workCenterName)){
			resultBean.setSuccess(false);
			resultBean.setMessage("WorkCenterName is not correct");
			return resultBean;
		}
		
		List<WorkCenter> wcList = routeStepDataHandler.getAttachedWorkCenterList(stepKey);
		for(WorkCenter wc : wcList){
			if(workCenterName.equals(wc.getInnerData().getName())){
				isAttached = true;
				break;
			}
		}
		
		if(! isAttached){
			resultBean.setSuccess(false);
			resultBean.setMessage("WorkCenter[" + workCenterName + "] is not attached on the route");
			return resultBean;
		}
		
		

		return resultBean;
	}

	@Override
	public ResultBean save(Unit obj) {
		
		ResultBean resultBean = new ResultBean();
		
		if(obj == null){
			resultBean.setSuccess(false);
			resultBean.setMessage("NULL Object is not allowed");
			return resultBean;
		}
		
		
		
		return resultBean;
	}

	@Override
	public ResultBean update(Unit obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean delete(Unit obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean saveBatch(List<Unit> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Unit> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Unit> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Unit getById(long id) {
		// TODO Auto-generated method stub
		return null;
	}

}
